﻿Imports ACompany.OrderProcessing.Model
Imports ACompany.OrderProcessing.Services
Imports System.Text
Imports StructureMap

Public Class CreateOrderPresenter

    Private view As ICreateOrderView
    Private OrderService As IOrderService
    Private ProductService As IProductService
    Private CustomerService As ICustomerService

    Public Sub New(ByVal view As ICreateOrderView)
        Me.New(view, ObjectFactory.GetInstance(Of IOrderService)(), ObjectFactory.GetInstance(Of IProductService)(), ObjectFactory.GetInstance(Of ICustomerService)())
    End Sub

    Public Sub New(ByVal view As ICreateOrderView, ByVal orderService As IOrderService, _
                   ByVal ProductService As IProductService, ByVal CustomerService As ICustomerService)
        Me.view = view
        Me.OrderService = orderService
        Me.ProductService = ProductService
        Me.CustomerService = CustomerService
    End Sub

    Public Sub Display()

        clearInfoAndErrorMessages()

        Dim Products As List(Of Product)
        Products = ProductService.FindAll
        view.Products = Products
        view.SelectProductButtonEnabled = (Products.Count > 0)

        Dim Customers As List(Of Customer)
        Customers = CustomerService.FindAll        
        view.Customers = Customers
        view.SelectCustomerButtonEnabled = (Customers.Count > 0)

        DisplayErrorsWith(Customers, Products)

        If (Products.Count = 0) And _
            (Customers.Count = 0) Then
            view.CreateOrderButtonEnabled = False
        End If

    End Sub

    Public Sub PlaceOrder()

        clearInfoAndErrorMessages()

        If view.Order.GetBrokenRules.Count = 0 Then
            OrderService.Place(view.Order)
            view.InfoMessage("Order placed successfully")
            view.clearOrder()                        
        Else
            DisplayErrorsIn(view.Order)
        End If
    End Sub

    Public Sub AddProductBy(ByVal ProductId As Guid)

        clearInfoAndErrorMessages()

        If Not ProductId = Guid.Empty Then

            Dim product As Product = ProductService.FindBy(ProductId)

            If product Is Nothing Then
                view.ErrorMessage(String.Format("No product found with Id {0}.", ProductId))
            Else
                view.Order.Add(product)
                view.OrderItems = view.Order.Items
            End If
        Else
            view.ErrorMessage(String.Format("Invalid product Id {0}.", ProductId))
        End If
    End Sub

    Public Sub SetCustomerBy(ByVal CustomerId As Guid)

        clearInfoAndErrorMessages()

        If Not CustomerId = Guid.Empty Then
            Dim customer As Customer = CustomerService.FindBy(CustomerId)

            If customer Is Nothing Then
                view.ErrorMessage(String.Format("No Customer found with Id {0}", CustomerId))

            Else
                view.Order.Customer = customer
                view.Order.DespatchAddress = customer.Address

                view.CustomerName = customer.Name
                view.Street = customer.Address.Street
                view.City = customer.Address.City
                view.County = customer.Address.County
                view.PostCode = customer.Address.PostCode

            End If
        Else
            view.ErrorMessage(String.Format("Invalid Customer Id {0}", CustomerId))
        End If


    End Sub

    Private Sub DisplayErrorsIn(ByVal Order As Order)

        Dim sbErrorMessage As New StringBuilder

        sbErrorMessage.Append("There were some problems when trying to place this Order:")
        sbErrorMessage.Append(Environment.NewLine)
        sbErrorMessage.Append(Environment.NewLine)

        For Each BrokenRule As Infrastructure.BrokenRule In Order.GetBrokenRules
            sbErrorMessage.Append(BrokenRule.Rule)
            sbErrorMessage.Append(Environment.NewLine)
        Next

        view.ErrorMessage(sbErrorMessage.ToString)
    End Sub

    Private Sub DisplayErrorsWith(ByVal Customers As IList(Of Customer), ByVal Products As IList(Of Product))

        Dim sbErrorMessage As New StringBuilder

        If Customers.Count = 0 Then
            sbErrorMessage.Append("Please add a customer before creating an order.")
            sbErrorMessage.Append(Environment.NewLine)
        End If

        If Products.Count = 0 Then
            sbErrorMessage.Append("Please add a product before creating an order.")
            sbErrorMessage.Append(Environment.NewLine)
        End If

        view.ErrorMessage(sbErrorMessage.ToString)
    End Sub

    Private Sub clearInfoAndErrorMessages()
        view.ErrorMessage(String.Empty)
        view.InfoMessage(String.Empty)
    End Sub
End Class
